package com.adderis.soft.domain.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.adderis.soft.domain.dao.IApplicationsDao;
import com.adderis.soft.domain.model.Applications;

@Repository("applicationsDao")
public class ApplicationsDaoImpl implements IApplicationsDao {

	private static final Log log = LogFactory.getLog(ApplicationsDaoImpl.class);

	Calendar calendar = Calendar.getInstance();
	java.util.Date now = calendar.getTime();
	Timestamp nowtimesstamp = new java.sql.Timestamp(now.getTime());;

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * {@inheritDoc}
	 */
	public void persist(Applications transientApplications) {
		log.debug("Persisting NomCommerial instance");
		try {
			transientApplications.setCreatetimestamp(nowtimesstamp);
			transientApplications.setStatutcode("A");
			transientApplications.setUpdatetimesstamp(nowtimesstamp);
			entityManager.persist(transientApplications);

			log.debug("persist successful");

		} catch (RuntimeException re) {
			log.error("persist failed", re);
		}

	}

	public void remove(Applications persistentApplications) {
		log.debug("removing Applications instance");
		try {
			persistentApplications.setStatutcode("D");
			persistentApplications.setDeletiontimesstamp(nowtimesstamp);
			entityManager.merge(persistentApplications);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.debug("remove failed", re);
		}

	}

	public void remove(Integer ApplicationsId) {
		this.remove(this.findById(ApplicationsId));

	}

	public Applications merge(Applications detachedApplications) {
		log.debug("merging Applications instance");
		try {
			detachedApplications.setUpdatetimesstamp(nowtimesstamp);
			Applications result = entityManager.merge(detachedApplications);
			entityManager.merge(detachedApplications);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			return null;
		}
	}

	public Applications findById(Integer id) {
		log.debug("getting Applications instance with id: " + id);
		try {
			Applications instance = entityManager.find(Applications.class, id);
			log.debug("findById successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("findById failed", re);
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */

	@SuppressWarnings("unchecked")
	public List<Applications> findAll() {
		log.debug("getting all Applications instance");
		try {
			Query query = entityManager
					.createQuery("SELECT c FROM Applications c where c.statutcode ='A'");
			List<Applications> applicationsList = (List<Applications>) query
					.getResultList();
			log.debug("findAll successful");
			return applicationsList;

		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			return new ArrayList<Applications>();
		}

	}

	@SuppressWarnings("unchecked")
	public List<Applications> search(String searchString) {
		log.debug("Search Applications instances with search string: "
				+ searchString);
		try {
			Query query = entityManager.createQuery("select nc from Applications nc where nc.Application like :searchString");
			query.setParameter("searchString", searchString);
			List<Applications> applicationsList = (List<Applications>) query
					.getResultList();
			log.debug("search successful");
			return applicationsList;
		} catch (RuntimeException re) {
			log.error("search failed", re);
			return new ArrayList<Applications>();
		}
	}

}
